package library.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import library.dao.IlibraryDao;
import library.db.DbConnection;
import library.pojo.Book;
import library.pojo.BookType;
import library.pojo.BorrowHistory;
import library.pojo.User;
import library.pojo.UserType;

public class UserTypeDaoImpl implements IlibraryDao{

	@Override
	public void save(Book book) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Book book) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Book> get(String isbn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(String isbn) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Book> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addBookType(BookType bookType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteBookType(String idx) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBookType(BookType bookType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BookType getBookType(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BookType> findAllBookType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteUser(String username) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getUser(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 *增加用户类型
	 */
	@Override
	public void addUserType(UserType userType) {
		Connection conn = DbConnection.getConnection();
		String sql = "insert into user_type values(?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userType.getId());
			ps.setString(2, userType.getuName());
			ps.setString(3, userType.getUserType());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 *删除用户类型
	 */
	@Override
	public void deleteUserType(String userType) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "delete from user_type where user_type = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userType);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, null);
		}
		
	}

	/**
	 *修改用户的用户类型
	 */
	@Override
	public void updateUserType(UserType userType) {
		Connection conn = DbConnection.getConnection();
		String sql = "update user_type set user_type=? where uname=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userType.getUserType());
			ps.setString(2, userType.getuName());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 *查询一个用户的用户类型
	 */
	@Override
	public UserType getUserType(String uname) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user_type where uname = ?";
		UserType userType = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uname);
			rs = ps.executeQuery();
			while (rs.next()) {
				String id = rs.getString(1);
				String uName = rs.getString(2);
				String usertype = rs.getString(3);
				userType = new UserType(id, uName, usertype);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return userType;
	}

	/**
	 *查询所有用户类型
	 */
	@Override
	public List<UserType> findAllUserType() {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user_type";
		UserType userType = null;
		List<UserType> userTypeList = new ArrayList<UserType>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String id = rs.getString(1);
				String uname = rs.getString(2);
				String usertype = rs.getString(3);
				userType = new UserType(id, uname, usertype);
				userTypeList.add(userType);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return userTypeList;
	}


	@Override
	public void deleteBorrowHistory(String isbn , String borrowUser) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void addBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BorrowHistory getBorrowHistory(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BorrowHistory> findAllBorrowHistory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTypeName(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

}
